
var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var helperOpen = require("helper/open");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var mallUrl = require("url/mall");

var button_addPro = ui("button_addPro");

var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据

var noData = [{"template":2,"labelContent":"店铺还没有商品"}];
var addShare = ui("$").add("share","source://view/reuse/share.ui", 0, 0);
//当前页码
var pageNum = 1;

/* 头部 */
var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
var labelTitle = ui(addheader+".labelTitle");
labelTitle.text='我的店铺';
headerLeft.on("touch", function() {
	app.closePage();
});
//右上角添加+图标
var headerRight = ui(addheader + ".headerRight");
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/header/search.png";
imgNext.visible = true;
//andriod返回键
page.on("back", function() {
    app.closePage();
});
var doWhat = null;
page.on("loaded", function(){
	doWhat = page.getData().doWhat;
	loadShopData();
});

page.on("result", function(data){
	if(data.mall_edit_product || data.mall_add_product 
			|| data.mall_product_chage){
		loadFirstProductPage();
	}
	if(data && data.shop_order_list){
		refreshShopData();
	}
});

page.on("mall_on_shelf_product", function(data, e) {
	loadFirstProductPage();
})

page.on("mall_off_shelf_product", function(data, e) {
	loadFirstProductPage();
})

var shopId = null;
var auditStatus = null; //店铺审核状态:1-未审核,2-审核通过,3-审核失败
function loadShopData() {
	http.post(mallUrl.url.shop.myShop, {}, function(data) {
		var mallShop = data.mallShop;
		if(mallShop){
			shopId = mallShop.id;//店铺id
			auditStatus = mallShop.auditStatus;//审批状态
		}else{
			nf.toast("您还没有开店！");
		}
		if(mallShop){
			mallShop.template = 1;
			listdata.removeAll();
			listdata.addOne(mallShop);
			listview.refreshItems();
			loadFirstProductPage();
		}
		if(doWhat == "addMallProduct"){
			if(auditStatus!="2"){
				nf.toast("该店铺还未审核通过！");
				return;
			}
			helperOpen.open("source://view/qshop/manage/product/addProduct.ui",{shopId:shopId});
		}
	});
}

//加载商品--第一页
function loadFirstProductPage() {
	if(shopId== null){
		return;
	}
	pageNum=1;
	http.post(mallUrl.url.product.listOnShelfProducts4MyShop, {shopId: shopId, page: pageNum}, function(data) {
		var productPage = data.productPage;
		if(productPage){
			if(listdata.getCount()>1){
				listdata.removeRange(1, listdata.getCount()-1);
			}
			if(productPage.content.length){
				listdata.addData(productPage.content);//加载商品列
			}else{
				listdata.addData(noData);//没有商品
			}
			listview.refreshItems();
		}		
	});
}
//加载商品--下一页
function loadNextProductPage() {
	if(shopId== null){
		return;
	}
	pageNum ++;
	http.post(mallUrl.url.product.listOnShelfProducts4MyShop, {shopId: shopId, page: pageNum}, function(data) {
		var productPage = data.productPage;
		if(productPage){
			if(productPage.content.length){
				listdata.addData(productPage.content);//加载商品列
				listview.refreshItems();
			}
		}		
	});
}

function refreshShopData() {
	httpSilence.post(mallUrl.url.shop.myShop, {}, function(data) {
		var mallShop = data.mallShop;
		if(mallShop){
			mallShop.template = 1;
			listdata.updateOne(0,mallShop);
			listview.refreshItems();
		}
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		loadFirstProductPage();
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		loadNextProductPage();
		listview.rebound();//复位【一定要做】
	}
});

button_addPro.on("touch",'' ,300,function(data, e) {
	if(shopId != null){
		if(auditStatus!="2"){
			nf.toast("该店铺还未审核通过！");
			return;
		}
		helperOpen.open("source://view/qshop/manage/product/addProduct.ui",{shopId:shopId});
	}else{
		nf.toast("您还没有开店！");
	}
	
});
headerRight.on("touch", function(){
	helperOpen.open("source://view/qshop/manage/product/myProduct.ui",{shopId:shopId});
});
app.on("fireShopFromUpdate",function(d){
	if(d.logo&&d.logo!==""){
		imageView_logo.source = d.logo;
	}
    if(d.name && d.name!==""){
    	label_shopName.text = d.name;
	}
});
